# 爬取永康人事考试信息
import requests
from lxml import etree
import xlwt
from xlwt import Workbook
import datetime

book = Workbook(encoding='utf-8')
url = 'http://www.sdsgwy.com/city/list-yongkang-1.html'
sheet1 = book.add_sheet('永康人事考试')

response = requests.get(url)
response.encoding = 'gbk'
xml = etree.HTML(response.text)
biaoti_list = xml.xpath('//li[@class="mainlist_li"]/span/a/text()')
src_list = xml.xpath('//li[@class="mainlist_li"]/span/a/@href')

for i in range(0, len(biaoti_list)):
    sheet1.write(i, 0, biaoti_list[i])
    sheet1.write(i, 8, src_list[i])
    response = requests.get(src_list[i])
    response.encoding = 'gbk'
    xml = etree.HTML(response.text)
    time = xml.xpath('//div[@class="mainNewsInfo"]/text()')
    sheet1.write(i, 6, time[0][-19:-3])

now_time = str(datetime.datetime.now())[0:10]
title = r'永康人事考试' + now_time + '.xlsx'
book.save(title)
